package com.djhu.aspect.limit;

import com.djhu.aspect.log.Logging;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.lang.reflect.Method;


/**
 * @author by zw on 2018/5/9.
 */
@Aspect
public class AspectLimit {
    private static Logger logger = LoggerFactory.getLogger(Aspect.class.getName());

    @Autowired
    private RedisTemplate redisTemplate;

    @Pointcut("@annotation(com.djhu.aspect.limit.ControlLimit)")
    public void pointLimit(){
    }



    @Around("pointLimit()")
    public void limit(ProceedingJoinPoint proceedingJoinPoint){
        long beginTime = System.currentTimeMillis();
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        //获取被拦截的方法
        Method method = signature.getMethod();
        //获取被拦截的方法名
        String methodName = method.getName();
        String key = null;
        ControlLimit controlLimit = method.getAnnotation(ControlLimit.class);
        if (StringUtils.isEmpty( controlLimit.key() )){
            key = signature.getDeclaringTypeName() +  methodName ;
        }

        try {
            proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {

        }
    }

}
